package cn.chenfeifx.core.controls;

import cn.chenfeifx.core.common.SimpleControl;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;

public class CFForm extends GridPane {

    public CFForm() {
        setHgap(20);
        setVgap(20);
        setMaxSize(USE_PREF_SIZE, USE_PREF_SIZE);
    }

    public CFForm(double hgap, double vgap) {
        setHgap(hgap);
        setVgap(vgap);
        setMaxSize(USE_PREF_SIZE, USE_PREF_SIZE);
    }

    public void addTitle(String text, int rowIndex) {
        add(SimpleControl.getLabel(text, SimpleControl.LabelEnum.H4), 0, rowIndex);
    }

    public void add(Node node, int rowIndex) {
        add(null, node, rowIndex, HPos.RIGHT, VPos.TOP);
    }

    public void add(String text, Node node, int rowIndex) {
        add(text, node, rowIndex, HPos.RIGHT, VPos.TOP);
    }

    public void addNodes(Node[] nodes, int rowIndex) {
        addNodes(null, nodes, rowIndex);
    }

    /**
     * 将多个node添加到FlowPane
     */
    public void addNodes(String text, Node[] nodes, int rowIndex) {
        FlowPane flowPane = new FlowPane(nodes);
        flowPane.hgapProperty().bind(hgapProperty());
        flowPane.vgapProperty().bind(vgapProperty().divide(2));
        add(text, flowPane, rowIndex, HPos.RIGHT, VPos.TOP);
    }

    public void add(String text, Node node, int rowIndex, HPos textHPos, VPos textVPos) {
        if (text != null) {
            Label label = SimpleControl.getLabel(text);
            label.setWrapText(false);
            label.setPrefHeight(32);
            label.setMinSize(USE_PREF_SIZE, USE_PREF_SIZE);
            GridPane.setHalignment(label, textHPos);
            GridPane.setValignment(label, textVPos);
            add(label, 0, rowIndex);
        }
        add(node, 1, rowIndex);
    }

}
